import { _decorator, Component, Node, Label, Button, Vec3, v3 } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { IapButtonItem } from '../ShopPanel/IapButtonItem';
import { DevConfig } from '../../../../scripts/managers/DevConfig';
import { find } from 'cc';
import { MSResolve, MSRToString_th_m } from '../../../../scripts/libs/utils/date';
import { lm } from '../../LobbyManager';
import { forkJoin, Observable } from 'rxjs';
import { TweenToStart } from '../../../../scripts/libs/rxjs/cc3/TweenStart';
const { ccclass, property } = _decorator;

@ccclass('RemoveAdsPanel')
export class RemoveAdsPanel extends BasePanel {

    @property(Node)
    mainNode: Node = null;

    @property(Button)
    closeButton: Button = null;

    @property(IapButtonItem)
    iapButtonItem: IapButtonItem = null;

    @property(Node)
    layoutNode: Node = null;

    willBind(): void {
        this.iapButtonItem.data.value = DevConfig.iap.noads_0;
        // this.adaptive();        
        this.mainNode.setScale(.5, .5, .5);
    }

    useObserves() {
        return [
            BindButton(this.closeButton).subscribe(this.closeButtonClicked.bind(this)),
            this.iapButtonItem.successCommand.subscribe(this.onBuySuccess.bind(this)),
        ];
    }

    onLoadView(): void {
        this.refresh();
    }

    // adaptive() {
    //     this.mainNode.setScale(v3(Vec3.ONE).multiplyScalar(lm.getAdaptiveScale()));
    // }

    openPanelAnim(): Observable<any> {
        return forkJoin([
            TweenToStart(this.ensureUIOpacity(), .2, { opacity: 255 }, { easing: 'expoOut' }),
            TweenToStart(this.mainNode, .4, { scale: lm.getAdaptiveScaleV3() }, { easing: 'backOut' }),
        ]);
    }

    closePanelAnim(): Observable<any> {
        this.ensureUIOpacity();
        return forkJoin([
            TweenToStart(this.ensureUIOpacity(), .2, { opacity: 0 }, { easing: 'expoIn' }),
            TweenToStart(this.mainNode, .2, { scale: v3(.5, .5, .5) }, { easing: 'backIn' }),
        ]);
    }

    closeButtonClicked() {
        RemoveAdsPanel.CloseProcess(null, 'close');
    }

    onBuySuccess() {
        RemoveAdsPanel.CloseProcess(null, 'purchased');
    }

    refresh() {
        let info = DevConfig.iap.noads_0;
        for (let i = 0; i < this.layoutNode.children.length; i++) {
            let itemNode = this.layoutNode.children[i];
            if (info.provide[itemNode.name] > 0) {
                itemNode.active = true;
                let valueLabelNode = find('label', itemNode);
                if (valueLabelNode != null) {
                    if (['inf_heart_sec', 'noads_sec'].includes(itemNode.name)) {
                        let msr = MSResolve(info.provide[itemNode.name] * 1000);
                        valueLabelNode.getComponent(Label).string = MSRToString_th_m(msr);
                    } else {
                        valueLabelNode.getComponent(Label).string = `x${info.provide[itemNode.name] ?? 0}`;
                    }
                }
            } else {
                itemNode.active = false;
            }
        }
    }

}

